package com.wjf.shortlink.shortlink.dao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wjf.shortlink.shortlink.dao.entity.LinkDeviceStatsDAO;
import com.wjf.shortlink.shortlink.dto.req.ShortLinkGroupStatsReqDTO;
import com.wjf.shortlink.shortlink.dto.req.ShortLinkStatsReqDTO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.HashMap;
import java.util.List;

//短链接访问设备统计持久层
public interface LinkDeviceStatsMapper extends BaseMapper<LinkDeviceStatsDAO> {
    //获取短链接指定日期内的访问设备统计
    @Select("SELECT" +
            "    tlds.device," +
            "    SUM(tlds.cnt) AS cnt," +
            "FROM " +
            "    link tl INNER JOIN " +
            "    link_device_stats tlds ON tl.full_short_url = tlds.full_short_url " +
            "WHERE " +
            "    tlds.full_short_url = #{param.fullShortUrl} " +
            "    AND tl.gid = #{param.gid} " +
            "    AND tl.del.flag = '0' " +
            "    AND tl.enable_status = #{param.enableStatus} " +
            "    AND tlds.date BETWEEN #{param.startDate} AND #{param.endDate} " +
            "GROUP BY " +
            "    tlds.full_short_url, tl.gid, tlds.device;"
    )
    List<LinkDeviceStatsDAO> listDeviceStatsByShortLink(@Param("param")ShortLinkStatsReqDTO shortLinkStatsReqDTO);

    //根据分组获取指定日期内设备监控数据
    @Select("SELECT " +
            "     tlds.browser, " +
            "     SUM(tlds.cnt) AS cnt " +
            "FROM " +
            "    link tl INNER JOIN " +
            "    link_device_stats tlds ON tl.full_short_url = tlds.full_short_url " +
            "WHERE " +
            "    tl.gid = #{param.gid} " +
            "    AND tl.del_flag = '0' " +
            "    AND tl.enable_status = '0' " +
            "    AND tlds.date BETWEEN #{param.startDate} AND #{param.endDate} " +
            "GROUP BY " +
            "     tl.gid, tlds.device;")
    List<LinkDeviceStatsDAO> listDeviceStatsByGroup(@Param("param") ShortLinkGroupStatsReqDTO shortLinkGroupStatsReqDTO);
}
